<!DOCTYPE html>
<html lang="en" class="page-fill">
<head>
    <meta charset="UTF-8"/>
    <title>警员登录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="警情闭环管控系统">
    <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="caseSystem/css/oksub.css">
    <link rel="stylesheet" href="caseSystem/lib/layui/css/layui.css">
</head>
<body class="page-fill" background="caseSystem/images/loginBackground.jpg" style="background-repeat:no-repeat ;background-size:100% 100%;background-attachment: fixed;">
<div class="page-fill" id="login">
    <form class="layui-form">
        <div class="login_face" style="height: 110px;width: 125px"><img style="height: 100%;width: 100%;" src="caseSystem/images/login.png"></div>

        <div class="layui-form-item input-item">
            <label for="policeId">警号</label>
            <input type="text" lay-verify="required" name="policeId"  autocomplete="off" id="policeId" class="layui-input">
        </div>
        <div class="layui-form-item input-item">
            <label for="password">密码</label>
            <input type="password" lay-verify="required|password" name="password"  autocomplete="off" id="password" class="layui-input">
        </div>
        <div class="layui-form-item input-item captcha-box" id="loginCode" style="display: none">
            <label for="captcha">验证码</label>
            <input id="captcha" type="text" name="captcha" autocomplete="off" id="captcha" maxlength="4" class="layui-input">
            <div class="img ok-none-select" id="captchaImg"></div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-block" lay-filter="login" lay-submit="">登录</button>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="caseSystem/lib/layui/layui.js"></script>
<script src="caseSystem/js/jquery.min.js"></script>

<script>
    if(window != top){
        top.location.href=location.href;
    };

    // 获取MainUri
    const mainUri = function () {
        let pathName = document.location.pathname;
        let index = pathName.substr(1).indexOf("/");
        let result = pathName.substr(0, index + 1);
        return result;
    }();

    layui.use(["form", "okGVerify", "okUtils", "okLayer","okCookie","okMd5", 'layer'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let okGVerify = layui.okGVerify;
        let okUtils = layui.okUtils;
        let okLayer = layui.okLayer;
        let okMd5 = layui.okMd5;
        var layer = layui.layer

        //次方法需要放在layui中才能使用
        function showloading(t) {
            if (t) {//如果是true则显示loading
                loading = layer.load(1, {
                    shade: [0.7, '#393D49'] //0.1透明度的白色背景
                });
            } else {//如果是false则关闭loading
                layer.closeAll('loading');
            }
        }

        /**
         * 初始化验证码
         */
        let verifyCode = new okGVerify("#captchaImg");

        /**
         * 数据校验
         */
        form.verify({
            password: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
            captcha: function (val) {
                if (verifyCode.validate(val) != "true") {
                    return verifyCode.validate(val)
                }
            }
        });
        /**
         * 表单提交
         */
        form.on("submit(login)", function (data) {
            data.field.password=okMd5(data.field.password);
            showloading(true)
            $.ajax({
                url: mainUri+"/vUserInfo/login",
                data: {
                    'policeId': data.field.policeId,
                    'password': data.field.password
                },
                type: "post",
                dataType: "json",
                success: function (response) {
                    showloading(false)
                    if (response.code == -9999) {
                        alert(response.msg);
                        if (response.data > 2) {
                            $("#loginCode").css("display", "block");
                            $("#captcha").attr("lay-verify", "required|captcha");
                        }
                    }
                    if (response.code == 0) {
                        let okData=response.data
                        window.sessionStorage.setItem("police", JSON.stringify(okData));
                        okLayer.greenTickMsg(response.msg, function () {
                            var uri="caseSystem/"
                            if(okData.grade==1){
                                uri+="deputyDirector.html"
                            }else if(okData.grade==2){
                                uri+="commandCenter.html"
                            }else if(okData.grade==3){
                                uri+="policeStation.html"
                            }else if(okData.grade==4){
                                uri+="policeOfficer.html"
                            }else if(okData.grade==9){
                                uri+="admin.html"
                            }
                            window.location = uri;
                        })
                    }
                },
                error: function () {
                    showloading(false)
                    alert("服务器错误");
                }
            });
            return false;
        });

        /**
         * 表单input组件单击时
         */
        $("#login .input-item .layui-input").click(function (e) {
            e.stopPropagation();
            $(this).addClass("layui-input-focus").find(".layui-input").focus();
        });

        /**
         * 表单input组件获取焦点时
         */
        $("#login .layui-form-item .layui-input").focus(function () {
            $(this).parent().addClass("layui-input-focus");
        });

        /**
         * 表单input组件失去焦点时
         */
        $("#login .layui-form-item .layui-input").blur(function () {
            $(this).parent().removeClass("layui-input-focus");
            if ($(this).val() != "") {
                $(this).parent().addClass("layui-input-active");
            } else {
                $(this).parent().removeClass("layui-input-active");
            }
        })
    });
</script>
</body>
</html>
